package com.oracle.controller;


import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.oracle.compents.ApiResponse;
import com.oracle.compents.CodeCompents;
import com.oracle.pojo.Student;
import com.oracle.service.CommandService;
import com.oracle.service.LoginStudentService;
import com.oracle.service.LoginTeacherService;
import com.oracle.service.StudentService;
import com.oracle.util.JJWTUtilsStudent;
import com.oracle.vo.DeleteRequest;
import com.oracle.vo.ImgVo;
import com.oracle.vo.StudentVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/student")
public class StudentController extends BaseStudentController{

    @Autowired
    private StudentService studentService;

    @Autowired
    private LoginStudentService loginStudentService;

    @Autowired
    private CommandService commandService;

    @Autowired
    private PasswordEncoder passwordEncoder;

    @PostMapping("/login")
    public ApiResponse findOne(@RequestBody Student student){
        ApiResponse<String> response = loginStudentService.login(student);
        if (!ObjectUtils.isEmpty(response)){
            response.setMsg(CodeCompents.ROLE_STUDENT+"");
            return response;
        }else {
            return null;
        }

    }

    @PostMapping("/register")
    public ApiResponse register(@RequestBody Student student){
        student.setStuPwd(passwordEncoder.encode(student.getStuPwd()));
        try {
            loginStudentService.register(student);
        } catch (Exception e) {
            return ApiResponse.failed(null) ;
        }

        return ApiResponse.success(null);
    }


    @GetMapping("/findStudent")
    public ApiResponse findStudent(){
        Student user = getUser();
        System.out.println(user);
        return ApiResponse.success(user);
    }

    @GetMapping("/findAllStudent")
    public ApiResponse findAllStudent(){
        List<Student> all = studentService.findAll();
        if (ObjectUtils.isEmpty(all)){
            return ApiResponse.failed(null);
        }else {
            return ApiResponse.success(all);
        }
    }

    @PostMapping("/search")
    public ApiResponse search(@RequestBody Student student,Integer pageNum , Integer pageSize){
        IPage<Student> studentIPage = studentService.searchStudent(student, pageNum, pageSize);
        if (ObjectUtils.isEmpty(studentIPage)){
            ApiResponse<Object> failed = ApiResponse.failed(null);
            failed.setMsg("学生模块，查询不到学生");
            return failed;
        }else {
            return ApiResponse.success(studentIPage);
        }
    }

    @GetMapping("/findOne")
    public ApiResponse findOne(Integer id){
        Student oneStudent = studentService.findOneStudent(id);
        if (ObjectUtils.isEmpty(oneStudent)){
            return ApiResponse.failed(null);
        }else {
            return ApiResponse.success(oneStudent);
        }
    }

    @PostMapping("/updataStudent")
    public ApiResponse updataStudent(@RequestBody StudentVo studentVo){
        System.out.println(studentVo.getStuImgList());
        List<String> studentList = strToJson(studentVo.getStuImgList());

        //1.将视图对象的值 倒入到持久层对象中
        Student student = new Student();
        BeanUtils.copyProperties(studentVo,student);
        //2.Json格式转换
        String string = JSONArray.toJSONString(studentList);
        //3.补充数据
        student.setStuHeadList(string);
        int i = studentService.updateStudent(student);
        if (i>0){
            String studentToken = JJWTUtilsStudent.getJwtToken(student);
            ApiResponse<Object> success = ApiResponse.success(studentToken);
            success.setMsg("学生修改信息成功");
            return success;
        }else {
            ApiResponse<Object> failed = ApiResponse.failed(null);
            failed.setMsg("修改失败");
            return failed;
        }
    }

    @PostMapping("/insert")
    public ApiResponse insert(@RequestBody Student student){
        int i = studentService.insertStudent(student);
        if (i>0){
            return ApiResponse.success(null);
        }else {
            return ApiResponse.failed(null);
        }
    }

    @PostMapping("/delete")
    public ApiResponse delete(@RequestBody DeleteRequest deleteRequest){
        try {
            List<Integer> ids = deleteRequest.getIds();
            int i = studentService.deleteStudent(ids);
            if (i>0){
                return ApiResponse.success(null);
            }else {
                return ApiResponse.failed(null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ApiResponse.failed(null);
        }
    }


    private List<String> strToJson(List<ImgVo> list){
        List<String> urlsList = new ArrayList<String>();
        //将goodsImgList数据进行json格式处理 => urljson数组
        list.forEach(x -> {
            urlsList.add(x.getUrl());
        });

        System.out.println("urls :" + urlsList);

        return urlsList;

    }
}
